package firstDemo;


import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class 微信聊天系统 {
	public static void main(String [] args)
	{
		new sever().start();
		new client().start();
	}
}
class client extends Thread
{
	public void run()
	{
		try {
			Scanner scanner=new Scanner(System.in);
			System.out.println("微信聊天系统欢迎你");
			System.out.println("请输入您的微信号");
			int n1=scanner.nextInt();
			System.out.println("请输入要发送的微信号");
			int n=scanner.nextInt();
			System.out.println("微信聊天系统启动");
			DatagramSocket socket=new DatagramSocket();
			while(true) {
			String string=scanner.next();
			if("exit".equals(string)) {
				System.out.println("已退出");
				socket.close();
				break;}
			byte[] buffer=string.getBytes();
			DatagramPacket packet=new DatagramPacket
			(buffer, buffer.length, InetAddress.getLocalHost(),n);
			socket.send(packet);}
			
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
}
class sever extends Thread{
	public void run()
	{
		try {
			Scanner scanner=new Scanner(System.in);
			
			DatagramSocket socket=new DatagramSocket(4260);
			byte[] buffer=new byte[64*1024];
			DatagramPacket packet=new DatagramPacket(buffer,
					buffer.length);
			while(true) {
			socket.receive(packet);
			int len=packet.getLength();
			System.out.println("收到"+packet.getAddress().
			getHostAddress()+"--发送的数据--"+new String(buffer,0,len));}
			
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
 		
	}
}